class Movie < ActiveRecord::Base

  has_many :reviews, :dependent => :destroy 
  @@ratings=%w[G PG PG-13 R NC-17]


  validates_presence_of :title
  validates_uniqueness_of :title
  validates_presence_of :description
  validates_length_of :description, :minimum=>10
  validates_inclusion_of :rating, :in => @@ratings

  def self.ratings
    @@ratings
  end

  def appropriate_for_birthdate?(birthdate)
    today=Time.now
    # subtract difference in years; -1 if your birthday hasn't occured this year
    age=today.year - birthdate.year - (today.yday < birthdate.yday ? 1 : 0)
    case rating
    when "G"
      true
    when "PG"
      true
    when "PG-13"
      age>=13
    when "R"
      age>=17
    when "NC-17"
      age>=17
    else
      true
    end
  end
  def self.find_all_appropriate_for_birthdate(birthdate)
    self.find(:all).select do |m|
      m.appropriate_for_birthdate?(birthdate)
    end
  end
end